package net.uhuijia.utils;

import javax.servlet.http.HttpServletRequest;

import net.uhuijia.appbase.model.BaseExample;
import net.uhuijia.appbase.model.Page;
import net.uhuijia.enums.MoreInfo;

public class IticConst {



    // 用户名密码字段对应
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";
    //程序入口
    public static final String PORTAL = "portal";
    //public static final String PORTAL_DEFAULT = PropertyHelper.getString("app.portal");
    // 分页参数名称
    public static final String RECORD_NOW = "start";
    public static final String PAGE_SIZE = "length";
    /** SESSION 常量 **/
    // SESSION中RSA私钥
    public static final String PRIVATE_KEY = "privateKey";
    // SESSION中用户
    public static final String PS_SESSION_USERINFO_KEY = "PS_USERINFO";

    /** 返回结果常量 **/
    public static final String RESULT = "result";
    public static final String DATA = "data";
    public static final String TOTAL_RECORDS = "iTotalRecords";
    public static final String DISPLAY_RECORDS = "iTotalDisplayRecords";

    //开始时间、结束时间后缀
    public static final String START_TIME_SUFFIX = " 00:00:00";
    public static final String END_TIME_SUFFIX =  " 23:59:59";
    public static final String YYYY_MM_DD =  "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM_SS =  "yyyy-MM-dd HH:mm:ss";

    private IticConst() {
    }


    /**
     * 处理请求中的P分页参数并设置给example（如果没有传start和length，默认10条）
     *
     * @param request
     * @return
     * @throws Exception
     *             getCleanParams
     * @throws Exception
     *             parseInt
     */
    public static <T extends BaseExample> T handlePageQueryDefault(HttpServletRequest request, T example) {
        Page page = null;
        String recordNow = getCleanParams(request, IticConst.RECORD_NOW);
        String pageSize = getCleanParams(request, IticConst.PAGE_SIZE);
        if (StringHelper.isNotEmpty(recordNow) && StringHelper.isNotEmpty(pageSize)) {
            page = new Page();
            //如果数据库是mysql的话
            //if( "mysql".equals(PropertyHelper.getString("app.db.type")) ){
                recordNow= NumberStringHelper.mulString(recordNow, pageSize);
            //}
            page.setRecordNow(Integer.parseInt(recordNow));
            page.setPageSize(Integer.parseInt(pageSize));
        }else{
            page = new Page();
            page.setRecordNow(0);
            page.setPageSize(10);
        }
        example.setPage(page);
        return example;
    }

    /**
     * 获取request中的指定参数，并进行trim处理.
     *
     * @param request
     * @param paramName
     * @return
     * @throws
     */
    public static  String getCleanParams(HttpServletRequest request, String paramName) {
        String result = null;
        String param = request.getParameter(paramName);
        result = StringHelper.getCleanString(param);
        return result;
    }

    /**
     * 判断是否还有更多
     * @param request
     * @param count
     * @return
     */
    public static MoreInfo isMore(HttpServletRequest request, int count){
        String recordNow = getCleanParams(request, IticConst.RECORD_NOW);
        String pageSize = getCleanParams(request, IticConst.PAGE_SIZE);
        recordNow =  StringHelper.isEmpty(recordNow)?"0":recordNow;
        pageSize = StringHelper.isEmpty(pageSize)?"10":pageSize;
        String addString = NumberStringHelper.addString(recordNow, "1");
        String mulString = NumberStringHelper.mulString(addString, pageSize);
        //比较两次数值大小
        MoreInfo moreInfo = !NumberStringHelper.isGreaterNoEqual(count+"", mulString)?MoreInfo.none:MoreInfo.have;
        return moreInfo;
    }
}
